﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Web.Mvc;
using System.Web.UI;
using WinnerCMS.BLL;
using WinnerCMS.BLL.PageFactory.TagModule;
using WinnerCMS.Model;
using UDT = WinnerCMS.BLL.PageFactory.Pages.UDT;

namespace WinnerCMS.WebFactory
{
    public class PublicPage<TModel> : PublicPage where TModel : class
    {
        private ViewDataDictionary<TModel> _viewData;
        public new AjaxHelper<TModel> Ajax { get; set; }
        public new HtmlHelper<TModel> Html { get; set; }

        public new TModel Model
        {
            get { return ViewData.Model; }
        }

        [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public new ViewDataDictionary<TModel> ViewData
        {
            get
            {
                if (this._viewData == null)
                {
                    SetViewData(new ViewDataDictionary<TModel>());
                }
                return this._viewData;
            }
            set { SetViewData(value); }
        }

        public override void InitHelpers()
        {
            base.InitHelpers();
            Ajax = new AjaxHelper<TModel>(ViewContext, this);
            Html = new HtmlHelper<TModel>(ViewContext, this);
        }

        protected override void SetViewData(ViewDataDictionary viewData)
        {
            this._viewData = new ViewDataDictionary<TModel>(viewData);
            base.SetViewData(this._viewData);
        }
    }

    /// <summary>
    ///     模板用的基类
    /// </summary>
    public class PublicPage : ViewPage
    {
        /// <summary>
        ///     栏目Id
        /// </summary>
        public int CategoryId { get; set; }
 
        /// <summary>
        ///     是否处于开发模式
        /// </summary>
        public bool IsDev { get; set; }

        protected override void OnPreInit(EventArgs e)
        {
            FillClassInfo();

            //IsDev = Request.Headers["dev"] != null;

            IsDev = true;
        }

        public virtual void FillClassInfo()
        {
            if (Request.QueryString["CategoryId"] != null)
            {
                CategoryId = int.Parse(Request.QueryString["CategoryId"]);
            }
            else if (Page.RouteData.Values.ContainsKey("CategoryId"))
            {
                CategoryId = Convert.ToInt32(Page.RouteData.Values["CategoryId"]);
            }
        }

        #region 重写Render

        protected override void Render(HtmlTextWriter writer)
        {
            var winner = new StringBuilder();
            using (TextWriter tempWriter = new StringWriter())
            {
                base.Render(new HtmlTextWriter(tempWriter));
                winner.Append(tempWriter);
            }

            //if (IsDev)
            //{
            //    if (CategoryInfo != null)
            //        TagEngine.ReplaceM(winner, CategoryInfo.ModelId);
            //    else
            //        TagEngine.ReplaceM(winner, 0);
            //}
            TagEngine.ReplaceM(winner, 0);

            UDT.Transact(winner);

            winner.AppendLine(@"<!--
            本网站由WinnerCMS 7 构建
            QQ:53854651
            EMail:winner2050@126.com
            -->");


            writer.Write(winner.ToString());
        }

        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }
    }
}